package com.kob.backend.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.backend.mapper.UserMapper;
import com.kob.backend.pojo.User;
import com.kob.backend.service.user.account.RegisterService;
import com.kob.backend.utils.Result;
import com.kob.backend.utils.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result register(String username, String password, String confirmedPassword) {
        if (username == null) {
            return Result.fail(ResultEnum.USERNAME_EMPTY);
        }
        if (password == null || confirmedPassword == null) {
            return Result.fail(ResultEnum.PASSWORD_EMPTY);
        }

        username = username.trim();
        if (username.length() == 0) {
            return Result.fail(ResultEnum.USERNAME_EMPTY);
        }

        if (password.length() == 0 || confirmedPassword.length() == 0) {
            return Result.fail(ResultEnum.PASSWORD_EMPTY);
        }

        if (username.length() > 100) {
            return Result.fail(ResultEnum.USERNAME_LENGTH);
        }

        if (password.length() > 100 || confirmedPassword.length() > 100) {
            return Result.fail(ResultEnum.PASSWORD_LENGTH);
        }

        if (!password.equals(confirmedPassword)) {
            return Result.fail(ResultEnum.PASSWORD_CONFIRM);
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<User> users = userMapper.selectList(queryWrapper);
        if (!users.isEmpty()) {
            return Result.fail(ResultEnum.USERNAME_EXIST);
        }

        String encodedPassword = passwordEncoder.encode(password);
        String[] photos = {"https://tse1-mm.cn.bing.net/th/id/OIP-C.evx5DFA7xvZ1zkRvsbETawHaHa?pid=ImgDet&w=474&h=474&rs=1",
                "https://tse2-mm.cn.bing.net/th/id/OIP-C.V6fd81z1SmZgPrSUSQCm_AHaHa?w=214&h=214&c=7&r=0&o=5&dpr=1.3&pid=1.7",
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.5ITviDYmKR2-iDuS_sIZowAAAA?rs=1&pid=ImgDetMain",
                "https://tse4-mm.cn.bing.net/th/id/OIP-C.hRHOC03RBwo1Qjc_mqcvdQHaHa?w=212&h=213&c=7&r=0&o=5&dpr=1.3&pid=1.7"
        };
        Random random = new Random();
        String photo = photos[random.nextInt(photos.length)];

        User user = new User(
                null,
                username,
                username,
                encodedPassword,
                null,
                null,
                null,
                photo,
                1500,
                0,
                null);
        userMapper.insert(user);

        return Result.success();
    }
}
